﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Sale
{
    public partial class 采购原料标书录入 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int billid = 0;
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out billid);
                }
                if (billid > 0)
                {
                    this.BillID.Text = billid.ToString();
                    this.SqlDataSource1.SelectParameters["BillID"].DefaultValue = this.BillID.Text;
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                else
                {
                    DateTime d = DateTime.Today;
                    this.Date1.Text = d.ToLongDateString();
                    this.NianDu.Text = d.Year.ToString();
                    this.JiDu.Text = (((d.Month-1) / 3) + 1).ToString();
                }
            }
        }
        protected void Button_Save_Click(object sender, EventArgs e)
        {
            this.MingCheng.Text = this.MingCheng.Text.Trim();
            if (this.MingCheng.Text == "")
            {
                this.Message.Text = "·必须录入“名称”";
                return;
            }
            DateTime d;
            if (!DateTime.TryParse(this.Date1.Text, out d))
            {
                this.Message.Text = "·“录入日期”录入有误";
                return;
            }
            if (this.BillID.Text == "" || this.BillID.Text == "0")
            {
                this.SqlDataSource1.InsertParameters["名称"].DefaultValue = this.MingCheng.Text;
                this.SqlDataSource1.InsertParameters["编号"].DefaultValue = this.BianHao.Text;
                this.SqlDataSource1.InsertParameters["年度"].DefaultValue = this.NianDu.Text;
                this.SqlDataSource1.InsertParameters["季度"].DefaultValue = this.JiDu.Text;
                this.SqlDataSource1.InsertParameters["日期"].DefaultValue = d.ToShortDateString();
                this.SqlDataSource1.InsertParameters["备注"].DefaultValue = this.BeiZhu.Text;
                //SqlConnection Conn = new SqlConnection();
                //Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                //DataSet ds = new DataSet();
                //SqlDataAdapter sda = new SqlDataAdapter("select count(*) from 采购原料标书 where 年度='" + this.NianDu.Text + "' and 季度='" + this.JiDu.Text + "'", Conn);
                //sda.Fill(ds);
                //if (ds.Tables[0].Rows[0][0].ToString() == "0")
                //{
                    if (this.SqlDataSource1.Insert() > 0)
                    {
                        this.Message.Text = "·保存成功";
                        this.Button_MX.OnClientClick = "parent.swin2_load('/Sale/采购原料中标.aspx?id=" + this.BillID.Text + "',840,422);return false;";
                        this.Button_MX.Visible = true;
                    }
                //}
                //else
                //{
                //    this.Message.Text = "·该季度的标书已经存在";
                //}
            }
            else
            {
                this.SqlDataSource1.UpdateParameters["BillID"].DefaultValue = this.BillID.Text;
                this.SqlDataSource1.UpdateParameters["名称"].DefaultValue = this.MingCheng.Text;
                this.SqlDataSource1.UpdateParameters["编号"].DefaultValue = this.BianHao.Text;
                this.SqlDataSource1.UpdateParameters["年度"].DefaultValue = this.NianDu.Text;
                this.SqlDataSource1.UpdateParameters["季度"].DefaultValue = this.JiDu.Text;
                this.SqlDataSource1.UpdateParameters["日期"].DefaultValue = d.ToShortDateString();
                this.SqlDataSource1.UpdateParameters["备注"].DefaultValue = this.BeiZhu.Text;
                if (this.SqlDataSource1.Update() > 0)
                {
                    this.Message.Text = "·保存成功";
                }
                else
                {
                    this.Message.Text = "·保存失败";
                }
            }
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            this.BillID.Text = ((int)e.Command.Parameters[0].Value).ToString();
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (Convert.IsDBNull(e.Command.Parameters[1].Value))
            {
                this.BillID.Text = "0";
            }
            else
            {
                this.MingCheng.Text = (string)e.Command.Parameters[1].Value;
                this.BianHao.Text = (string)e.Command.Parameters[2].Value;
                this.NianDu.Text = ((int)e.Command.Parameters[3].Value).ToString();
                this.JiDu.Text = ((int)e.Command.Parameters[4].Value).ToString();
                this.Date1.Text = ((DateTime)e.Command.Parameters[5].Value).ToLongDateString();
                this.BeiZhu.Text = (string)e.Command.Parameters[6].Value;
                this.Button_MX.Visible = true;
                this.Button_MX.OnClientClick = "parent.swin2_load('/Sale/采购原料中标.aspx?id=" + this.BillID.Text + "',840,422);return false;";
            }
        }
    }
}
